package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.ContactsFriendEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.friend.service.IAddContactsFriendService;
import com.forlove.app.qselect.friend.service.impl.AddContactsFriendServiceImpl;
import com.forlove.app.qselect.friend.view.AddContactsFriendView;

import java.util.HashMap;

/**
 * Created by yjl on 2017/10/25.
 */

public class AddContactsFriendPresenter extends IBasePresenter<IAddContactsFriendService> {
    private AddContactsFriendView addContactsFriendView;
    private IAddContactsFriendService addContactsFriendService;

    public AddContactsFriendPresenter(AddContactsFriendView addContactsFriendView) {
        this.addContactsFriendView = addContactsFriendView;
        if (addContactsFriendService == null) {
            addContactsFriendService = new AddContactsFriendServiceImpl();
            setService(addContactsFriendService);
        }
    }


    public void loadContactFriend() {
        addContactsFriendService.loadData(ServerManager.ACTION_GET_CONTACTS_FRIEND, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                ContactsFriendEntity friendListEntity = new ContactsFriendEntity();
                friendListEntity = ParseHelper.getModel(result, "data", ContactsFriendEntity.class);
                if (friendListEntity != null) {
                    addContactsFriendView.updateFriendListView(friendListEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (addContactsFriendView != null) {
                        addContactsFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (addContactsFriendView != null) {
                        addContactsFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
